<?php

class Form {
    
    private $form;
    
    public function __construct($form) {
        $this->form = $form;
    }
    
    public function minlength($field, $length, $name = '') {
        if (strlen($this->form[$field]) >= $length) return true;
        setsession('error','Campul '.(($name != '') ? $name : $field).' trebuie sa aiba minim '.$length.' caractere');
        return false;
    }
    
    public function notempty($field, $name = '') {
        if (!empty($this->form[$field])) return true;
        setsession('error','Campul '.(($name != '') ? $name : $field).' nu poate fi gol');
        return false;
    }
    
    public function _isset($field, $name = '') {
        if (isset($this->form[$field])) return true;
        setsession('error','Campul '.(($name != '') ? $name : $field).' trebuie completat');
        return false;
    }
    
    public function imageset($field, $name = '') {
        if (!empty($_FILES[$field])) return true;
        setsession('error','Campul '.(($name != '') ? $name : $field).' nu poate fi gol');
        return false;
    }
    
    public function checkemail($field, $name = '', $required = true) {
        if ($required == false && empty($this->form[$field])) return true;
        if(preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $this->form[$field]) == 1) return true;
        setsession('error','Campul '.(($name != '') ? $name : $field).' nu este valid');
        return false;
    }
    
}

